package cn.xfdzx.android.apps.shop.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ScrollView;

/**
 * @author gzk
 * @date 2020/4/14 0014
 * description：
 */
public class CustomScrollview extends ScrollView {
    private boolean isScrolledToTop;
    private boolean isScrolledToBottom;


    public CustomScrollview(Context context) {
        super(context);
    }

    public CustomScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 解决 由于子控件listview或者GridView的大小导致ScrollView自动滚动的问题
     *
     * @param rect
     * @return
     */
    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
        return 0;
    }
    public interface ScrollViewListener {
        void onScrolledToBottom();
        void onScrolledToTop();
    }
    private ScrollViewListener mScrollViewListener;

    public void setScanScrollChangedListener(ScrollViewListener ScrollViewListener) {
        mScrollViewListener = ScrollViewListener;
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (scrollY <= 0) {
            isScrolledToTop = clampedY;
            isScrolledToBottom = false;
        } else {
            isScrolledToTop = false;
            isScrolledToBottom = clampedY;
        }
        notifyScrollChangedListeners();
    }

    private void notifyScrollChangedListeners() {
        if (isScrolledToTop) {
            if (mScrollViewListener != null) {
                mScrollViewListener.onScrolledToTop();
            }
        } else if (isScrolledToBottom) {
            if (mScrollViewListener != null) {
                mScrollViewListener.onScrolledToBottom();
            }
        }
    }

}
